package com.manager.service.view.impl;


import com.manager.bean.PageInfo;
import com.manager.bean.view.StudentAccessView;
import com.manager.bean.view.StudentAccessViewExample;
import com.manager.mapper.view.StudentAccessViewMapper;
import com.manager.service.view.StudentAccessViewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("studentAccessViewService")
public class StudentAccessViewServiceImpl implements StudentAccessViewService {
    @Resource
    private StudentAccessViewMapper studentAccessViewDao;
    @Override
    public PageInfo<StudentAccessView> findPageInfo(Integer Id, String name, Integer pageIndex, Integer pageSize) {
        PageInfo<StudentAccessView> pi = new PageInfo<StudentAccessView>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        StudentAccessViewExample studentAccessViewExampleExample=new StudentAccessViewExample();
        StudentAccessViewExample.Criteria criteria=studentAccessViewExampleExample.createCriteria();
        if(name!=null&&name!=""){
            criteria.andNameLike("%"+name+"%");
        }
        if(Id!=null){
            criteria.andIdEqualTo(Id);
        }
        //获取总条数
        Integer totalCount = studentAccessViewDao.countByExample(studentAccessViewExampleExample);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //每一页显示学生信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            List<StudentAccessView> classInfos =studentAccessViewDao.selectByExample(studentAccessViewExampleExample);
            int start=(pi.getPageIndex()-1)*pi.getPageSize();
            int end=start+pi.getPageSize();
            if(end>classInfos.size()-1){
                end=classInfos.size();
            }
            classInfos=classInfos.subList(start,end);
            pi.setList(classInfos);
        }
        return pi;
    }

    @Override
    public List<StudentAccessView> getAll() {
        StudentAccessViewExample studentAccessViewExample=new StudentAccessViewExample();
        StudentAccessViewExample.Criteria criteria=studentAccessViewExample.createCriteria();
        return studentAccessViewDao.selectByExample(studentAccessViewExample);
    }
}
